package com.vol.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vol.entity.SysUser;
import com.vol.entity.UserInfo;
import com.vol.entity.pager.PageParams;
import com.vol.entity.pager.ResultPager;
import com.vol.mapper.SysUserMapper;
import com.vol.utils.RolesUtils;
import com.vol.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RolesUtils rolesUtils;
    @Autowired
    private UserContext userContext;

    public ResultPager<SysUser> getPageData(PageParams pageParams) {
        UserInfo userInfo = userContext.getUserInfo();
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Page<SysUser> page = new Page<>(pageParams.getPage(), pageParams.getRows());
        QueryWrapper<SysUser> qw = new QueryWrapper<SysUser>();
        if (pageParams.getOrder().equals("desc")) {
            qw.orderByDesc(pageParams.getSort());
        } else if (pageParams.getOrder().equals("asc")) {
            qw.orderByAsc(pageParams.getSort());
        }
        if (userInfo.getRole_Id() != 1) {
            List<Integer> list_RoleIds = rolesUtils.getAllChildRoles();
            for(Integer roleId:list_RoleIds){
                qw.or().eq("Role_Id",roleId);
            }
        }
        sysUserMapper.selectPage(page, qw);
        List<SysUser> records = page.getRecords();
        for (SysUser sysUser : records) {
            sysUser.setSbpwd(null);
            sysUser.setUserpwd(null);
        }
        ResultPager<SysUser> resultPager = new ResultPager<>();
        resultPager.setStatus(0);
        resultPager.setRows(records);
        resultPager.setTotal((int) page.getTotal());
        return resultPager;
    }

    public void saveOrUpdate(SysUser sysUser) {
        UserInfo userInfo = userContext.getUserInfo();
        if (sysUser.getUserId() != null) {//更新
            LambdaUpdateWrapper<SysUser> uw = new LambdaUpdateWrapper<>();
            uw.eq(SysUser::getUserId, sysUser.getUserId());
            uw.set(SysUser::getDeptids, sysUser.getDeptids());
            uw.set(SysUser::getEnable, sysUser.getEnable());
            uw.set(SysUser::getGender, sysUser.getGender());
            uw.set(SysUser::getHeadimageurl, sysUser.getHeadimageurl());
            uw.set(SysUser::getRemark, sysUser.getRemark());
            uw.set(SysUser::getRoleId, sysUser.getRoleId());
            uw.set(SysUser::getUsername, sysUser.getUsername());
            uw.set(SysUser::getUsertruename, sysUser.getUsertruename());

            sysUser.setModifyid(userInfo.getUser_Id());
            sysUser.setModifier(userInfo.getUserName());
            sysUser.setModifydate(new Date());
            sysUserMapper.update(sysUser, uw);
        } else {//添加
            sysUser.setSbpwd("e10adc3949ba59abbe56e057f20f883e");
            sysUser.setCreateid(userInfo.getUser_Id());
            sysUser.setCreator(userInfo.getUserName());
            sysUser.setCreatedate(new Date());
            sysUserMapper.insert(sysUser);
        }
    }

    @Transactional
    public void del(ArrayList<String> ids) {
        for (String id : ids) {
            sysUserMapper.deleteById(id);
        }
    }

}
